package com.example.demo;

import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.demo.common.realm.UserValidation;
import com.example.demo.modules.service.UserService;

@Configuration
public class CustomShiroConfiguration {
	@Autowired
	private UserService userService;
	// @Bean
	// public Realm realm() {
	// TextConfigurationRealm realm = new TextConfigurationRealm();
	// realm.setUserDefinitions("joe.coder=password,user\n" +
	// "jill.coder=password,admin");
	// realm.setRoleDefinitions("admin=read,write\n" + "user=read");
	// realm.setCachingEnabled(true);
	// return realm;
	// }

	@Bean
	public Realm validate() {
		return new UserValidation(userService);
	}

	@Bean
	public ShiroFilterChainDefinition shiroFilterChainDefinition() {
		DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
		chainDefinition.addPathDefinition("/", "anon");
		chainDefinition.addPathDefinition("/index", "anon");
		chainDefinition.addPathDefinition("/index.html", "anon");
		chainDefinition.addPathDefinition("/error", "anon");
		chainDefinition.addPathDefinition("/user/login", "anon");
		chainDefinition.addPathDefinition("/user/create", "anon");
		chainDefinition.addPathDefinition("/user/page/register", "anon");
		chainDefinition.addPathDefinition("/user/register", "anon");
		chainDefinition.addPathDefinition("/user/check/**", "anon");
		chainDefinition.addPathDefinition("/components/**", "anon");
		chainDefinition.addPathDefinition("/modules/**", "anon");
		// chainDefinition.addPathDefinition("/templates/**", "anon");
		chainDefinition.addPathDefinition("/**", "authc");
		chainDefinition.addPathDefinition("/logout", "logout");
		return chainDefinition;
	}
}
